﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Telerik.Web.UI;
using Medianamik.UI.Controls;
using Medianamik.Globalization;

namespace Medianamik.UI.ConfigManagement
{
    public partial class LanguageList : BasePage
    {
        #region Fields
        private const string PostBackEventArgument = "__EVENTARGUMENT";
        public string Message
        {
            get;
            private set;
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            var deleteWindow = new BaseModalWindow { ID = "addCultureWindow", Width = Unit.Pixel(450), Height = Unit.Pixel(200) };
            MyWindowManager.Windows.Add(deleteWindow);

            if (!IsPostBack)
            {
                GridLanguages.Rebind();
                deleteWindow.OnClientClose = "modalWindowCallback";
                deleteWindow.IconUrl = "/Medianamik/images/Icones/Ico_Ajouter.gif";
                deleteWindow.ReloadOnShow = true;
                deleteWindow.ShowContentDuringLoad = false;
                Message = Translator.TranslateJS("Medianamik_ConfigurationManagement", "ConfirmLanguageActivityChanged");
            }
            else
            {
                if (!string.IsNullOrEmpty(Request[PostBackEventArgument]))
                {
                    GridLanguages.Rebind();
                }
            }
        }

        protected void GridLanguages_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var item = e.Item as GridDataItem;
            if (item != null)
            {
                var medAct = item.FindControl("MedianamikActive") as CheckBox;
                if (medAct != null)
                {
                    var cultureField = item.FindControl("CultureName") as HiddenField;
                    if (cultureField != null && cultureField.Value == CurrentUICulture.Name)
                        medAct.Enabled = false;
                    else
                        medAct.Attributes.Add("onclick", "if(!confirmActivation()) { this.checked = !this.checked; return; }");
                }

                var cmAct = item.FindControl("ContentManagementActive") as CheckBox;
                if(cmAct != null)
                    cmAct.Attributes.Add("onclick", "if(!confirmActivation()) { this.checked = !this.checked; return; }");
            }
        }

        protected void GridLanguages_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            GridLanguages.DataSource = ConfigManager.GetAllCultures().Where(c => c.CultureName != string.Empty).OrderBy(c => c.Position);
        }

        protected void ActivityChanged(object sender, EventArgs e)
        {
            var checkBox = sender as CheckBox;
            if (checkBox != null)
            {
                var cell = checkBox.Parent as GridTableCell;
                if(cell != null)
                {
                    var item = cell.Parent as GridDataItem;
                    if (item != null)
                    {
                        var field = item.FindControl("CultureName") as HiddenField;
                        if (field != null)
                        {
                            var culture = ConfigManager.GetCulture(field.Value);
                            if (checkBox.ID == "MedianamikActive")
                                culture.MedianamikActive = checkBox.Checked;
                            else if (checkBox.ID == "ContentManagementActive")
                                culture.ContentManagementActive = checkBox.Checked;
                            else
                                throw new InvalidOperationException("Valeur invalide");
                            ConfigManager.SaveCulture(culture);
                            GridLanguages.Rebind();
                        }
                    }
                }
            }
        }

        protected void GridLanguages_RowDrop(object sender, GridDragDropEventArgs e)
        {
            var destId = (string)e.DestDataItem.GetDataKeyValue("CultureName");
            var originId = (string)e.DraggedItems[0].GetDataKeyValue("CultureName");            
            int position = e.DestDataItem.ItemIndex;

            var cultures = ConfigManager.GetAllCultures().OrderBy(c => c.Position).ToList();

            var reorderedCulture = cultures.FirstOrDefault(c => c.CultureName == originId);
            if(reorderedCulture != null
                && cultures.Count > position)
            {
                cultures.Remove(reorderedCulture);
                cultures.Insert(position,reorderedCulture);
                short i = 0;
                cultures.ForEach(c=>c.Position = i++);
                cultures.ForEach(c => ConfigManager.SaveCulture(c));
                
            }

            GridLanguages.Rebind();
        }
    }
}
